﻿using AutoMapper;
using BS.Domain.Cultivation;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.BreedingEnterprisesn
{
    public class InsertBreedingEnterprisesCommandHandler: IRequestHandler<CreateBreedingEnterpriseCommand,ApiResult>
    {
        private readonly IBreedingEnterprisesRepository _breedingEnterprisesRepository;

        private readonly ILogger<InsertBreedingEnterprisesCommandHandler> _logger;

        private readonly IMapper _mapper;

        public InsertBreedingEnterprisesCommandHandler(IBreedingEnterprisesRepository breedingEnterprisesRepository,
            ILogger<InsertBreedingEnterprisesCommandHandler> logger, IMapper mapper)
        {
            _breedingEnterprisesRepository = breedingEnterprisesRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult> Handle(CreateBreedingEnterpriseCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var breedingEnterprises = _mapper.Map<BreedingEnterprises>(request);
                var result= await _breedingEnterprisesRepository.InsertAsync(breedingEnterprises);
                return ApiResult.Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "InsertBreedingEnterprisesCommandHandler");
                return ApiResult.Fail(ex.Message);
            }
        }
    }
}
public class CreateBreedingEnterpriseCommand:IRequest<ApiResult>
{
    /// <summary>
    /// 养殖场
    /// </summary>
    public string Farm { get; set; } = string.Empty;
    /// <summary>
    /// 负责人
    /// </summary>
    public string Principal { get; set; } = string.Empty;
    /// <summary>
    /// 存栏数量
    /// </summary>
    public int BreedingStock { get; set; }
    /// <summary>
    /// 养殖信息ID
    /// </summary>
    public int BreedingId { get; set; }
}